#include <iostream>
#include <string>
#include <vector>

#include <sqlite3.h>

#include "libsqlitecpp.h"

using namespace std;

Database::Database(std::string dbPath)
{
    sqlite3_open(dbPath.c_str(), &(this->db));
}

Database::~Database()
{
    sqlite3_close(this->db);
}

Query
Database::prepare(std::string sql)
{
    Query query(this->db, sql);
    return query;
}

Query::Query(sqlite3 *db, std::string sql) : db(db), sql(sql) {}

Query::~Query()
{
    this->results.clear();
}

void
Query::execute()
{
    sqlite3_exec(
        this->db,
        this->sql.c_str(),
        (int (*)(void *, int, char **, char **))queryCallback,
        this,
        //&dbError
        NULL
        );
}

int
Query::getNumColumns() {
    return this->numColumns;
}

int
queryCallback(void *pArg, int argc, char **argv, char **columnNames)
{
    Query *q = (Query *)pArg;
    q->numColumns = argc;

    std::vector<std::string> row(argc);
    for (int i = 0; i < row.size(); i++) {
        std::string str(argv[i]);
        row.at(i) = str;
    }

    q->results.push_back(row);

    return 0;
}

